package com.school.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.school.admin.entity.MallSysPurview;
import com.school.admin.entity.MallSysPurviewRole;
import com.school.admin.entity.vo.PurviewSaveVo;
import com.school.admin.entity.vo.PurviewVo;
import com.school.admin.mapper.MallSysPurviewMapper;
import com.school.admin.service.MallSysPurviewRoleService;
import com.school.admin.service.MallSysPurviewService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 接口权限表 服务实现类
 * </p>
 *
 * @author sunpx
 * @since 2022-03-24
 */
@Service
public class MallSysPurviewServiceImpl extends ServiceImpl<MallSysPurviewMapper, MallSysPurview> implements MallSysPurviewService {

    @Autowired
    ApplicationContext applicationContext;

    @Resource
    MallSysPurviewMapper mallSysPurviewMapper;


    @Autowired
    MallSysPurviewRoleService mallSysPurviewRoleService;

    @Override
    public Page<PurviewVo> getApiAll(Page<Object> page, PurviewVo purviewVo) {
        Page<PurviewVo> allPurviewRolesAll = mallSysPurviewMapper.getAllPurviewRolesAll(page, purviewVo);
        List<PurviewVo> collect = allPurviewRolesAll.getRecords().stream().map(res -> {
            String roleNames = res.getRoleNames();
            if (!StringUtils.isEmpty(roleNames)) {
                res.setRole(Arrays.asList(roleNames.split(",")));
            }
            return res;
        }).collect(Collectors.toList());
        allPurviewRolesAll.setRecords(collect);
        //查询出以拥有的菜单
        return allPurviewRolesAll;
    }


    @Override
    @Transactional
    public void savePurview(PurviewSaveVo purviewVo) {
        Integer type = purviewVo.getType();

        // TODO 获取列表数据
        List<MallSysPurview> purviewList = purviewVo.getPurviewList();

        boolean b = this.saveOrUpdateBatch(purviewVo.getPurviewList());

        //判断是否添加成功
        if (b) {

            //获取保存后的id
            List<Long> collect =
                    purviewList.stream()
                            .map(MallSysPurview::getId)
                            .filter(Objects::nonNull)
                            .collect(Collectors.toList());

            // TODO 验证是否是追加
            if (type == 0) {
                /*追加*/
                mallSysPurviewRoleService.appendPurviewRole(collect, purviewVo.getRole());

            } else {
                //清除数据
                QueryWrapper<MallSysPurviewRole> deWrapper = new QueryWrapper<>();
                deWrapper.in("purview_id", collect);
                mallSysPurviewRoleService.remove(deWrapper);
                //重新插入
                List<MallSysPurviewRole> objects = new ArrayList<>();
                for (Long aLong : collect) {
                    for (String s : purviewVo.getRole()) {
                        objects.add(new MallSysPurviewRole().setPurviewId(aLong).setRoleId(Long.valueOf(s)).setCreateTime(new Date()));
                    }
                }
                mallSysPurviewRoleService.saveBatch(objects);

            }
        }
    }


    @Override
    public List<PurviewVo> queryRolePurview(String rid) {
        return baseMapper.queryRolePurview(rid);
    }
}
